`
https://leetcode.cn/problems/minimum-depth-of-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var minDepth = function (root) {
  // 记录当前的最大深度
  let curMaxDepth = Infinity

  const q = [root]

  let depth = 1

  // BFS
  while (q.length) {
    const sz = q.length
    for (let i = 0; i < sz; i++) {
      const cur = q.shift()
      // 超过最大深度，不用遍历了
      if (depth > curMaxDepth) break

      if (cur === null) continue

      // 遇到叶子节点，更新最小值
      if (cur.left === null && cur.right === null) {
        curMaxDepth = Math.min(curMaxDepth, depth)
        continue
      }

      q.push(cur.left)
      q.push(cur.right)
    }
    depth++
  }

  return curMaxDepth === Infinity ? 0 : curMaxDepth
};